package functions;

import main.ContingencyTable;

public class MutualInformationFunction extends GeneralFunction {

	public MutualInformationFunction(String name) {
		super(name);
	}

	@Override
	public double calculate(ContingencyTable t) {
		return (((t.f00() / t.N()) * log2((t.N() * t.f00())
				/ (t.f0p() * t.fp0())))
				+ ((t.f01() / t.N()) * log2((t.N() * t.f01())
						/ (t.f0p() * t.fp1())))
				+ ((t.f10() / t.N()) * log2((t.N() * t.f10())
						/ (t.f1p() * t.fp0()))) + ((t.f11() / t.N()) * log2((t
				.N() * t.f11()) / (t.f1p() * t.fp1()))))
				/ (-1.0 * (((t.f0p() / t.N()) * log2(t.f0p() / t.N())) + ((t
						.f1p() / t.N()) * log2(t.f1p() / t.N()))));
	}

	private double log2(double a) {
		return Math.log(a) / Math.log(2);
	}

}
